package controllers;

import java.io.File;
import java.io.IOException;
import java.util.List;

import models.Blogroll;

import org.apache.commons.io.FileUtils;

import play.Play;
import play.mvc.Controller;
import play.mvc.With;
import service.BlogrollService;

@With(Secure.class)
public class BlogrollAction extends Controller{
	
	/**
	 * 查询友情链接的页面
	 */
	@Check("InformationManage")
	public static void list(){
		List<Blogroll> blogrolls = BlogrollService.findAll();
		render(blogrolls);
	}
	
	/**
	 * 跳转向添加页面
	 */
	@Check("InformationManage")
	public static void blank(){
		render();
	}
	
	/**
	 * 添加友情链接
	 * @param bloTit  标题
	 * @param bloUrl  链接地址
	 * @param upload  上传的图片文件
	 */
	@Check("InformationManage")
	public static void create(String bloTit,String bloUrl,File upload){
		String[] ss = upload.getName().split("\\.");
		if(check(ss[1])){
			File file= Play.getFile("/public/logo/"+upload.getName());
				try {
					FileUtils.copyFile(upload, file);
					Blogroll blogroll = new Blogroll();
					blogroll.bloTit = bloTit;
					blogroll.bloUrl = bloUrl;
					blogroll.bloLogo = "/public/logo/"+upload.getName();
					BlogrollService.add(blogroll);
					flash.success("添加成功");
					list();
				} catch (IOException e) {
					System.out.println("类BlogAction中的create（）方法抛异常");
					e.printStackTrace();
					flash.success("添加失败");
					blank();
				}
			}
		else{
			flash.success("图片格式不对");
			blank();
		}
	}
	
	/**
	 * 检查图片格式
	 * @param ext  图片文件的格式
	 * @return
	 */
	@Check("InformationManage")
	public static boolean check(String ext){
		String[] exts = {"bmp","jpg","jpeg","png","gif","BMP","JPG","JPEG","PNG","GIF"};
		for (String str : exts) {
			if(str.equals(ext))
				return true;
		}
		return false;
	}
	
	/**
	 * 跳转向修改页面
	 * @param id       友情链接id
	 */
	@Check("InformationManage")
	public static void show(Long id){
		Blogroll blog = BlogrollService.findById(id);
		render(blog);
	}
	
	/**
	 * 修改友情链接的方法
	 * @param id       友情链接id
	 * @param bloTit   标题
	 * @param bloUrl   链接地址   
	 * @param upload   上传问的图片文件
	 */
	@Check("InformationManage")
	public static void save(Long id,String bloTit,String bloUrl,File upload){
		Blogroll blogroll = BlogrollService.findById(id);
		blogroll.bloTit = bloTit;
		blogroll.bloUrl = bloUrl;
		if(upload != null){
			String[] ss = upload.getName().split("\\.");
			if(check(ss[1])){
				File file = Play.getFile("/public/logo/"+upload.getName());
				try {
					FileUtils.copyFile(upload, file);
					blogroll.bloLogo = "/public/logo/"+upload.getName();
					BlogrollService.update(blogroll);
					flash.success("修改成功");
					list();
				} catch (IOException e) {
					System.out.println("类BlogrollAction中的sava（）方法抛异常");
					e.printStackTrace();
					flash.success("修改失败");
					show(id);
				}
			}else{
				flash.success("图片格式错误");
				show(id);
			}
		}else{
					blogroll.save();
					flash.success("修改成功");
					list();
		}
	}
	/**
	 * 删除友情链接的方法
	 * @param id  友情链接id
	 */
	@Check("InformationManage")
	public static void delete(Long id){
		try {
			BlogrollService.delete(id);
			flash.success("删除成功");
			list();
		} catch (Exception e) {
			System.out.println("类BlogrollAction中的delete（）方法抛异常");
			e.printStackTrace();
			flash.success("删除失败");
			list();
		}
	}
	
}
